﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Main.Structure
{
    public class Appointment
    {
        // Local variables:
        private DateTime _date; // Used for grabbing the day, month, date ticks, etc.
        private long _originalTicks; // The ticks of this appointment when it was first loaded.
        private string _notes; // Any notes tied to this appointment.
        private object syncObject = new object(); // To prevent cross-threading.

        /// <summary>
        /// Class constructor.
        /// Takes the ticks of the date of the appointment as well as any notes (optional)
        /// and assigns them to the variables noted.
        /// </summary>
        /// <param name="dateTick">The ticks corresponding to a date.</param>
        /// <param name="notes">Any notes associated with this appointment.</param>
        public Appointment(long dateTick, string notes)
        {
            _date = new DateTime(dateTick); // Creates a new DateTime using a Date.Ticks value.
            _originalTicks = dateTick;
            _notes = notes;
        }

        /// <summary>The month of the date of this appointment.</summary>
        public int Month
        {
            get
            {
                lock (syncObject)
                    return _date.Month;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(_date.Year, value, _date.Day, _date.Hour, _date.Minute, _date.Second);
            }
        }

        /// <summary>The date of the date of this appointment.</summary>
        public int Day
        {
            get
            {
                lock (syncObject)
                    return _date.Day;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(_date.Year, _date.Month, value, _date.Hour, _date.Minute, _date.Second);
            }
        }

        /// <summary>The year of the date of this appointment.</summary>
        public int Year
        {
            get
            {
                lock (syncObject)
                    return _date.Year;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(value, _date.Month, _date.Day, _date.Hour, _date.Minute, _date.Second);
            }
        }
        
        /// <summary>The hour of the date of this appointment.</summary>
        public int Hour
        {
            get
            {
                lock (syncObject)
                    return _date.Hour;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(_date.Year, _date.Month, _date.Day, value, _date.Minute, _date.Second);
            }
        }

        /// <summary>The minute of the date of this appointment.</summary>
        public int Minute
        {
            get
            {
                lock (syncObject)
                    return _date.Minute;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(_date.Year, _date.Month, _date.Day, _date.Hour, value, _date.Second);
            }
        }

        /// <summary>The second of the date of this appointment.</summary>
        public int Second
        {
            get
            {
                lock (syncObject)
                    return _date.Second;
            }
            set
            {
                lock (syncObject)
                    _date = new DateTime(_date.Year, _date.Month, _date.Day, _date.Hour, _date.Minute, value);
            }
        }

        /// <summary>The date of this appointment in ticks. Value retrieval only.</summary>
        public long Ticks
        {
            get
            {
                lock (syncObject)
                    return _date.Ticks;
            }
        }

        /// <summary>The date of this appointment in (MM/dd/yyyy) format. Value retrieval only.</summary>
        public string Date
        {
            get
            {
                lock (syncObject)
                    return _date.Date.ToString("MM/dd/yyyy");
            }
        }

        /// <summary>The day of the week of this appointment. Value retrieval only.</summary>
        public DayOfWeek DayOfWeek
        {
            get
            {
                lock (syncObject)
                    return _date.DayOfWeek;
            }
        }

        /// <summary>The time of day associated with this appointment. Value retrieval only.</summary>
        public string TimeOfDay
        {
            get
            {
                lock (syncObject)
                {
                    if (_date.Hour >= 12)
                        return "PM";
                    else { return "AM"; }
                }
            }
        }

        /// <summary>Notes associated with this appointment.</summary>
        public string Notes
        {
            get
            {
                lock (syncObject)
                    return _notes;
            }
            set
            {
                lock (syncObject)
                    _notes = value;
            }
        }
    }
}
